/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
function init( lang ) {
  loadHeaderTop( lang );

  // remove task name
  var h = document.getElementById('header');
  h.innerHTML = h.innerHTML.replace( /<strong>Task.+?,/ig, '' );
  
  loadMessages(lang);
  
  loadTasks();
  applyPermission();
  $('msg_select').disabled = true;
  
  // language list selection
  var dl = $('displayLangList');
  for ( var i=0; i<dl.length; i++ ) {
    if ( dl[i].value==lang ) {
      dl[i].selected = true;
      break;
    }
  }
}

function loadTasks() {
  var tl = $('taskList');
  var param = eval('$H({"action": "GetTasks" })').toQueryString();
  var json = sync(param);
  tl.innerHTML='';
  tl.appendChild(document.createElement('option')); // blank opt
  for (var i = 0; i < json.tasks.length; i++) {
    var opt = document.createElement('option');
    var t = json.tasks[i];
    opt.innerHTML = t.taskName +" (Target Lang: "+t.trgLang+")";
    opt.taskId = t.taskId;
    opt.privilege = -1;
    opt.value = t.taskNameShort;
    opt.description = t.description;
    opt.srcLang = t.srcLang;
    opt.trgLang = t.trgLang;
    opt.types = t.types;
    opt.currentPhase = t.currentPhase;
    tl.appendChild(opt);
  }
  // no task is defined
  if ( json.tasks.length==0 ) { 
    tl.style.width = '160px';
    tl.disable();
    $('taskDetail').innerHTML = "No task is currently defined.<br>Define a task from the admin menu.";
  }
}

function selectTask() {
  var tl = $('taskList');
  var opt = tl[tl.selectedIndex];
  $('msg_select').disabled = opt.privilege<=0;
  if ( tl.selectedIndex == 0 ) { $('msg_select').disabled = true; }
  
  if (!opt.innerHTML) {
    $('taskDetail').innerHTML = "";
    return;
  }
  $('taskDetail').innerHTML = [
      '<table cellpadding="3" cellspacing="0" border="0">',
      '<tr><td valign="top" nowrap><strong id="msg_task">'+msg( lang, "msg_task" )+'</strong></td>',
      '<td style="padding-bottom:5px;">'+opt.innerHTML+'</td></tr>',
      '<tr><td valign="top" nowrap><strong id="msg_answer_type">'+msg( lang, "msg_answer_type" )+'</strong></td>',
      '<td style="padding-bottom:5px;">'+opt.types.split(',').join(', ')+'</td></tr>',
      '<tr><td valign="top" nowrap><strong id="msg_description">'+msg( lang, "msg_description" )+'</strong>&nbsp;&nbsp;</td>',
      '<td style="padding-bottom:5px;">'+opt.description+'</td></tr>',
      '<tr><td valign="top" nowrap><strong id="msg_access">'+msg( lang, "msg_access" )+'</strong></td><td>',
      (opt.privilege>0?"Yes":"No"),
      '</td></tr>',
      '</table>'
      ].join('');
}

function applyPermission() {
  // enable admin buttons
  if (isAdmin) {
      var buttons = ['msg_tm','msg_um'];
      //'msg_upload_status',
      for ( var i=0; i<buttons.length; i++ ) { 
        $(buttons[i]).enable(); 
      }
  }
  
  // enable organizer buttons
  if (privilege==3 || isAdmin) {
      var buttons = ['msg_log_browser','msg_progress_browser',
                          'msg_pm','msg_user_stat','msg_exporter',
                          'msg_file_manager'];
      //'msg_upload_status',
      for ( var i=0; i<buttons.length; i++ ) { 
        $(buttons[i]).enable(); 
      }
  }
  
  var tl = $('taskList');
  if (isAdmin) {
    for (var j=0; j<tl.length;j++) {
      tl[j].privilege = 3;
    }
    return;
  }
  
  var param = eval('$H({"action": "GetPermission","userId": "'+userId+'" })').toQueryString();
  var json = sync(param);
  for (var i=0; i<json.permissions.length; i++) {
    var tid = json.permissions[i].taskId;
    for (var j=0; j<tl.length;j++) {
      if ( tl[j].taskId == tid ) {
        tl[j].privilege = json.permissions[i].privilege;
      }
    }
  }
}

function setDisplayLanguage(newLang) {
  lang=newLang;
  
  //This doesn't work for the IE!
  //... it seems it's not working for others either.
  var src = 'js/resources.js.jsp?taskNameShort=none&displayLang='+newLang;
  var sc = document.createElement('script');
  sc.type = 'text/javascript';
//  sc.onload = callback;
  sc.src = src;
  document.body.appendChild(sc);
  
  loadMessages(newLang);
  var param = eval('$H({"action":"UpdateDisplayLang", "userId": "'+userId+'","displayLang": "'+newLang+'" })').toQueryString();
  sync(param);
}

